/**
 * 检查测试期数状态的脚本
 */

const mysql = require('mysql2/promise');

const DB_CONFIG = {
    host: '43.143.253.188',
    port: 3306,
    user: 'nextjs',
    password: 'yEAiRE2jcxTyRBy5',
    database: 'nextjs',
    timezone: '+08:00'
};

async function checkTestRounds() {
    let connection;
    
    try {
        console.log('🔌 正在连接数据库...');
        connection = await mysql.createConnection(DB_CONFIG);
        console.log('✅ 数据库连接成功！\n');

        console.log('🔍 检查测试期数状态...');
        const [rounds] = await connection.execute(`
            SELECT id, gameType, roundNumber, startTime, endTime, status, createdAt
            FROM casino_rounds 
            WHERE roundNumber = 202
            ORDER BY gameType
        `);
        
        if (rounds.length > 0) {
            console.log(`📊 找到 ${rounds.length} 个期数编号为202的期数：`);
            rounds.forEach(round => {
                const gameTypeText = round.gameType === 1 ? '大小玩法' : '赤壁玩法';
                const statusText = round.status === 0 ? '下注中' : round.status === 1 ? '开奖中' : '已结算';
                console.log(`   • 期数ID: ${round.id}, 类型: ${gameTypeText}, 状态: ${statusText} (${round.status})`);
                console.log(`     开始时间: ${round.startTime}, 结束时间: ${round.endTime}`);
            });
        } else {
            console.log('❌ 没有找到期数编号为202的期数');
        }

        // 检查所有状态为"下注中"的期数
        console.log('\n🔍 检查所有状态为"下注中"的期数...');
        const [bettingRounds] = await connection.execute(`
            SELECT id, gameType, roundNumber, startTime, endTime, status
            FROM casino_rounds 
            WHERE status = 0
            ORDER BY createdAt DESC
            LIMIT 5
        `);
        
        if (bettingRounds.length > 0) {
            console.log(`📊 找到 ${bettingRounds.length} 个状态为"下注中"的期数：`);
            bettingRounds.forEach(round => {
                const gameTypeText = round.gameType === 1 ? '大小玩法' : '赤壁玩法';
                console.log(`   • 期数ID: ${round.id}, 类型: ${gameTypeText}, 期数编号: ${round.roundNumber}`);
                console.log(`     开始时间: ${round.startTime}, 结束时间: ${round.endTime}`);
            });
        } else {
            console.log('❌ 没有找到状态为"下注中"的期数');
        }

    } catch (error) {
        console.error('❌ 检查失败:', error.message);
        console.error(error.stack);
    } finally {
        if (connection) {
            await connection.end();
            console.log('\n🔌 数据库连接已关闭');
        }
    }
}

// 运行检查脚本
if (require.main === module) {
    checkTestRounds().catch(console.error);
}

module.exports = { checkTestRounds }; 